package com.xianrenit.xianrenpuzi.manager.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.xianrenit.xianrenpuzi.manager.mapper.*;
import com.xianrenit.xianrenpuzi.manager.service.ProductService;
import com.xianrenit.xianrenpuzi.model.dto.product.ProductDto;
import com.xianrenit.xianrenpuzi.model.dto.product.ProductSaveDto;
import com.xianrenit.xianrenpuzi.model.entity.product.*;
import com.xianrenit.xianrenpuzi.model.vo.product.ProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;
    @Resource
    ProductDetailsMapper productDetailsMapper;
    @Resource
    ProductSkuMapper productSkuMapper;
    @Resource
    BrandMapper brandMapper;
    @Resource
    CategoryMapper categoryMapper;
    @Override
    public PageInfo<ProductVo> list(ProductDto productDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductVo> productVoList = productMapper.selectByCondition(productDto);
        return new PageInfo<>(productVoList);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void bigSave(ProductSaveDto productSaveDto) {
        //解析dto中的数据 分别存储到对应的表中
        //1、product
        Product product = new Product();
        //BeanUtils:copyProperties 可以将参数1的属性值复制给参数2相同类型名称的属性
        BeanUtils.copyProperties(productSaveDto , product);
        product.setStatus(0);//上架状态：1上架 -1下架
        product.setAuditStatus(0); //审核状态： 1通过  -1驳回
        //新增后需要返回新增商品的id：
        productMapper.save(product);

        Long productId = product.getId();
        //3、product_details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(productId);
        productDetails.setId(productId);
        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
        //2、product_sku
        List<ProductSku> skuList = productSaveDto.getProductSkuList();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        skuList.forEach(productSku -> {
            productSku.setProductId(productId);
            //设置商品编码
            productSku.setSkuCode(snowflake.nextIdStr());
            //拼接设置商品标题：
            productSku.setSkuName(product.getName()+" "+
                    productSku.getSkuSpec());
            productSku.setSaleNum(0);
            productSku.setStatus(0);

        });
        productSkuMapper.batchSave(skuList);

    }

    @Override
    public ProductSaveDto getById(Long id) {
        //可以使用多表联查：结合resultMap自定义映射解析响应结果封装为ProductSaveDto对象
        //也可以分成多步查询：结合异步编排(开子线程查询)可以提高查询效率
        //影响服务器并发能力的因素：服务器线程数(线程数合适最好)、业务执行时长(缓存异步队排好)

        Product product = productMapper.selectById(id);

        ProductDetails productDetails = productDetailsMapper.selectById(id);

        List<ProductSku> productSkuList = productSkuMapper.selectBySpuId(id);

        ProductSaveDto productSaveDto = new ProductSaveDto();
        BeanUtils.copyProperties(product , productSaveDto);
        productSaveDto.setDetailsImageUrls(productDetails.getImageUrls());
        productSaveDto.setProductSkuList(productSkuList);


        //查询品牌名称  三级分类名称
        Brand brand = brandMapper.selectById(product.getBrandId());
        Category category1 = categoryMapper.selectById(product.getCategory1Id());
        Category category2 = categoryMapper.selectById(product.getCategory2Id());
        Category category3 = categoryMapper.selectById(product.getCategory3Id());


        productSaveDto.setBrandName(brand.getName());
        productSaveDto.setCategory1Name(category1.getName());
        productSaveDto.setCategory2Name(category2.getName());
        productSaveDto.setCategory3Name(category3.getName());

        return productSaveDto;
    }
    //当前业务执行成功  清空redis的 键 对应的缓存
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "spzx:cache:index" , key = "'hots'")
    @Override
    public void updateById(ProductSaveDto productSaveDto) {
        //前端提交的数据全部都更新：根据id
        //1、更新spu
        Product product = new Product();
        BeanUtils.copyProperties(productSaveDto , product);
        productMapper.update(product);

        //2、更新详情图
        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(product.getId());
        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());

        productDetailsMapper.update(productDetails);
        //3、更新sku列表
        List<ProductSku> skuList = productSaveDto.getProductSkuList();
        skuList.forEach(productSku -> {
            productSku.setSkuName(product.getName()+" "+ productSku.getSkuSpec());

            productSkuMapper.update(productSku);
        });

    }
    //审批
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        product.setAuditStatus(auditStatus);
        product.setAuditMessage("商品参数错误");
//        SpzxLoginAuthInterceptor.THREAD_LOCAL.get().getId() 获取当前审批管理员的id
//        product.setAuditAdminId()
        productMapper.update(product);
    }
    //上下架
    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        productMapper.update(product);

        //sku上下架
        //根据spu Id作为条件 更新它所有的sku的上下架状态
        productSkuMapper.updateByProductId(id , status);
    }
}
